package com.glorysoft.sunnypda.ui.setting

import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import com.glorysoft.sunnypda.BR
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseActivity
import com.glorysoft.sunnypda.base.BaseViewModel
import com.glorysoft.sunnypda.base.Constant
import com.glorysoft.sunnypda.databinding.ActivitySettingBinding
import com.glorysoft.sunnypda.utils.ChangeLanguageHelper
import com.glorysoft.sunnypda.utils.PopWindowsUtils
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity
import kotlinx.android.synthetic.main.activity_setting.*
import java.util.*

/**
 * FileName: SettingActivity
 * Author: XJH
 * Date: 2022/1/11 18:00
 * Description:
 * History:
 */
class SettingActivity : BaseActivity<SettingViewModel,ActivitySettingBinding>(),
    View.OnClickListener {
    private var mLanguagePop : PopWindowsUtils.GeneralPop? = null
    override fun layoutId(): Int {
        return R.layout.activity_setting
    }

    override fun initVariableId(): Int {
        return BR.settingViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.system_settings))
        viewModel.initVersion(this)
        rlIP.setOnClickListener(this)
        rlFileIP.setOnClickListener(this)
        flLanguage.setOnClickListener(this)

        when (Constant.LANGUAGE) {
            "中文", "Chinese", "Tiếng trung quốc" -> tvLanguage.text = getString(R.string.chinese)
            "英语", "Tiếng anh", "English" -> tvLanguage.text = getString(R.string.english)
            "越南语", "Việt nam", "Vietnamese" -> tvLanguage.text = getString(R.string.vietnamese)
        }
    }

    override fun initData() {

    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.rlIP ->{
                viewModel.showScanDialog(this)
            }
            R.id.rlFileIP ->{
                viewModel.showFileDialog(this)
            }
            R.id.flLanguage->{
                if (mLanguagePop == null){
                    mLanguagePop = PopWindowsUtils.getOneChoosePop(this, flLanguage.width, viewModel.mLanguageList,
                            ivLanguage,object : PopWindowsUtils.PopTextAndPositionCallback{
                        override fun onSuccess(codeS: String, potion: Int) {
                            viewModel.updateLanguage(this@SettingActivity,codeS)
                        }

                    })

                }

                if (mLanguagePop!!.isShowing) {
                    mLanguagePop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivLanguage)
                    mLanguagePop?.showAsDropDown(flLanguage, 0, 0)
                }
            }
        }
    }
}